package br.com.massai.model.entity;

import java.math.BigDecimal;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "GROUP_ITEM")
@NamedQueries({
	@NamedQuery(name = "GroupItem.find", query = "SELECT g FROM GroupItem g"),
	@NamedQuery(name = "GroupItem.duplicate", query = "SELECT COUNT(g) FROM GroupItem g WHERE g.productionGroup =:productionGroup AND g.serviceItem =:serviceItem")
})
public class GroupItem extends GenericEntity {

	private static final long serialVersionUID = -1747166580363814952L;
	
	@Id
	@SequenceGenerator(name = "seqGroupItem", sequenceName = "seq_group_item", allocationSize = 1)
	@GeneratedValue(generator = "seqGroupItem", strategy = GenerationType.SEQUENCE)
	private Long id;

	@Column(name = "UNIT_VALUE", nullable = false, precision = 10, scale = 2, columnDefinition = "NUMERIC(10,2) DEFAULT 0")
	private BigDecimal unitValue;

	@ManyToOne(cascade = CascadeType.REFRESH, optional = true)
	@JoinColumn(name = "PRODUCTION_GROUP_ID", referencedColumnName = "ID")
	private ProductionGroup productionGroup;

	@ManyToOne(cascade = CascadeType.REFRESH, optional = false, fetch = FetchType.EAGER)
	@JoinColumn(name = "SERVICE_ITEM_ID", referencedColumnName = "ID")
	private ServiceItem serviceItem;
	
	@Column(name = "FLAG_FIXED")
	private boolean flagFixed;
	
	public GroupItem() {
		this.unitValue = BigDecimal.ZERO.setScale(GenericEntity.SCALE_VALUE);
		this.productionGroup = new ProductionGroup();
		this.serviceItem = new ServiceItem();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) return false;
		if(obj instanceof GroupItem){
			GroupItem g = (GroupItem) obj;
			if(g.getId() != null)
				if(g.getId().equals(this.getId())) return true;
			if(g.getServiceItem() != null && g.getServiceItem().getId() != null)
				if(g.getServiceItem().equals(this.getServiceItem())) return true;
		}

		return false;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public BigDecimal getUnitValue() {
		return unitValue;
	}

	public void setUnitValue(BigDecimal unitValue) {
		this.unitValue = unitValue;
	}

	public ProductionGroup getProductionGroup() {
		return productionGroup;
	}

	public void setProductionGroup(ProductionGroup productionGroup) {
		this.productionGroup = productionGroup;
	}

	public ServiceItem getServiceItem() {
		return serviceItem;
	}

	public void setServiceItem(ServiceItem serviceItem) {
		this.serviceItem = serviceItem;
	}

	public boolean isFlagFixed() {
		return flagFixed;
	}

	public void setFlagFixed(boolean flagFixed) {
		this.flagFixed = flagFixed;
	}
	
}
